package io.shallwe.scanner.manual;

import org.tio.client.intf.ClientAioListener;
import org.tio.core.ChannelContext;
import org.tio.core.intf.Packet;

import com.jfinal.kit.LogKit;

/**
 * 手动扫描枪时间监听器
 * @author 王小伟
 * @date 2016-06-16 08:21:11
 */
public class ManualScannerListener implements ClientAioListener{

	@Override
	public void onAfterConnected(ChannelContext context, boolean isConnected, boolean isReconnect) throws Exception {
		String ip = context.getServerNode().getIp();
		if(isConnected) {
			switch(ip) {
				case "172.30.31.57":
					LogKit.warn("\r\n连接1#上线手动扫描枪成功");
					break;
				case "172.30.31.58":
					LogKit.warn("\r\n连接2#上线手动扫描枪成功");
					break;
				case "172.30.31.56":
					LogKit.warn("\r\n连接1#下线手动扫描枪成功");
					break;
				case "172.30.31.59":
					LogKit.warn("\r\n连接2#下线手动扫描枪成功");
					break;
				case "172.30.31.60":
					LogKit.warn("\r\n连接3#下线手动扫描枪成功");
					break;
				case "172.30.31.61":
					LogKit.warn("\r\n连接4#下线手动扫描枪成功");
					break;
				default:
					break;
			}
			
		}else {
			switch(ip) {
			case "172.30.31.57":
				LogKit.warn("\r\n连接1#上线手动扫描枪失败,稍后会再次尝试.....");
				break;
			case "172.30.31.58":
				LogKit.warn("\r\n连接2#上线手动扫描枪失败,稍后会再次尝试.....");
				break;
			case "172.30.31.56":
				LogKit.warn("\r\n连接1#下线手动扫描枪失败,稍后会再次尝试.....");
				break;
			case "172.30.31.59":
				LogKit.warn("\r\n连接2#下线手动扫描枪失败,稍后会再次尝试.....");
				break;
			case "172.30.31.60":
					LogKit.warn("\r\n连接3#下线手动扫描枪失败,稍后会再次尝试.....");
					break;
			case "172.30.31.61":
				LogKit.warn("\r\n连接4#下线手动扫描枪失败,稍后会再次尝试.....");
				break;
			default:
				break;
		}
			
		}
	}

	@Override
	public void onAfterDecoded(ChannelContext context, Packet packet, int packetSize) throws Exception {
		
	}

	@Override
	public void onAfterHandled(ChannelContext context, Packet packet, long cost) throws Exception {
		
	}

	@Override
	public void onAfterReceivedBytes(ChannelContext context, int receivedBytes) throws Exception {
		String ip = context.getServerNode().getIp();
		switch(ip) {
			case "172.30.31.57":
				LogKit.info("\r\n收到1#上线手动扫描枪的反馈数据.....");
				LogKit.info("\r\n收到1#上线手动扫描枪的数据长度为："+receivedBytes);
				break;
			case "172.30.31.58":
				LogKit.info("\r\n收到2#上线手动扫描枪的反馈数据.....");
				LogKit.info("\r\n收到2#上线手动扫描枪的数据长度为："+receivedBytes);
				break;
			case "172.30.31.56":
				LogKit.info("\r\n收到1#下线手动扫描枪的反馈数据.....");
				LogKit.info("\r\n收到1#下线手动扫描枪的数据长度为："+receivedBytes);
				break;
			default:
				break;
		}
		
	}

	@Override
	public void onAfterSent(ChannelContext context, Packet packet,  boolean isSentSuccess) throws Exception {
		
	}

	@Override
	public void onBeforeClose(ChannelContext context, Throwable packet, String remark, boolean isRemove) throws Exception {
		String ip = context.getServerNode().getIp();
		switch(ip) {
			case "172.30.31.57":
				LogKit.warn("\r\n与1#上线手动扫描枪连接断开");
				break;
			case "172.30.31.58":
				LogKit.warn("\r\n与2#上线手动扫描枪连接断开");
				break;
			case "172.30.31.56":
				LogKit.warn("\r\n与1#下线手动扫描枪连接断开");
				break;
			default:
				break;
		}
	}

}